package com.app.view;

import android.content.Context;
import android.content.res.Resources;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.FrameLayout;

import com.app.application.QXApplication;
import com.app.utils.CommonUtil;

import java.util.List;

import phone.qyg.com.qyg.R;


/**
 * 作者：lblbh on 2017/2/13 14:43
 * 邮箱：lanbuhan@163.com
 * 网名: 前行
 * 功能：所有view的基类
 */
public abstract class BaseViewStateLayout extends FrameLayout {
	
	public String DELFAUTSTATE = "delfautState";
	private View sucessView = null;
	private View progressView = null;
	private View erroeView = null;
	private View emptyView =null;
	private BaseViewState mState = BaseViewState.STATE_PROGRESS;

//	public BaseViewStateLayout(Context context, AttributeSet attrs,
//			int defStyleAttr, int defStyleRes) {
//		super(context, attrs, defStyleAttr, defStyleRes);
//		initCreateView();
//	}

	public BaseViewStateLayout(Context context, AttributeSet attrs,
			int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		initCreateView();
	}

	public BaseViewStateLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
		initCreateView();
	}

	public BaseViewStateLayout(Context context) {
		super(context);
		initCreateView();
	}
	
	/**
	 *  设置空数据界面
	 * @param context
	 * @param emptyView
	 */
	public BaseViewStateLayout(Context context,View emptyView) {
		super(context);
		this.emptyView = emptyView;
		initCreateView();
		
	}

	private void initCreateView(){
		LayoutParams params =null;
		
		if(params == null){
			Resources resources = QXApplication.getContext().getResources();
			DisplayMetrics dm = resources.getDisplayMetrics();
//			float density = dm.density;
			int height = dm.heightPixels;
			params = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);

		}
		
		if(progressView == null){
			progressView = View.inflate(QXApplication.getContext(), R.layout.base_view_state_progress, null);
			progressView.setVisibility(View.VISIBLE);
			addView(progressView,params);
		}
		
		
		
		if(erroeView == null){
			erroeView = View.inflate(QXApplication.getContext(), R.layout.base_view_state_erroeview, null);
			
			erroeView.findViewById(R.id.btn_reload).setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					mState = BaseViewState.STATE_PROGRESS;
					stateView();
					loadDataRefreshView();
				}
			});
			erroeView.setVisibility(View.VISIBLE);
			addView(erroeView,params);
		}
		
		
		if(emptyView == null){
			emptyView = View.inflate(QXApplication.getContext(), R.layout.base_view_state_empty, null);
			emptyView.setVisibility(View.VISIBLE);
		}
		
		addView(emptyView,params);
		
		if(sucessView == null){
			sucessView = obtionView();
		}
		if(sucessView != null){
			addView(sucessView,params);
			sucessView.setVisibility(View.VISIBLE);
		}else{
			throw new IllegalArgumentException("The sucessView view null,add sucess view");
		
		}
		
		stateView();

		loadDataRefreshView();
	}
	

	private void loadDataRefreshView() {

		new Thread(){
			public void run() {
				Object object = obtionData();
				mState= dataOrViewSate(object);
				CommonUtil.runOnUIThread(new Runnable() {
					
					@Override
					public void run() {
						stateView();
					}
				});
			};
		}.start();
	}


	private BaseViewState dataOrViewSate(Object object){
		BaseViewState state = null;
		if(object != null){
			if(object instanceof List){
				List<?> list = (List<?>) object;
				if(list.size() >0){
					state = BaseViewState.STATE_SUCESS;
				}else{
					state = BaseViewState.STATE_EMPTY;
				}
			}else if(object instanceof String){
				String strState = (String) object;
				if(strState.equals(DELFAUTSTATE)){
					state = BaseViewState.STATE_PROGRESS;
				}else{
					state = BaseViewState.STATE_SUCESS;
				}
			}else{
				state = BaseViewState.STATE_SUCESS;
			}
		}else{
			state = BaseViewState.STATE_ERROE;
		}
		return state;
	}


	public void stateView() {

		sucessView.setVisibility(View.GONE);
		progressView.setVisibility(View.GONE);
		erroeView.setVisibility(View.GONE);
		emptyView.setVisibility(View.GONE);
		switch (mState.getValue()) {
			case 1:
				progressView.setVisibility(View.VISIBLE);
				break;
			case 2:
				erroeView.setVisibility(View.VISIBLE);
				break;
			case 3:
				emptyView.setVisibility(View.VISIBLE);
				break;
			case 4:
				sucessView.setVisibility(View.VISIBLE);
				break;

		}
	}
	
	public void setThreadOrState(BaseViewState state){
		mState = state;
	}


	public enum BaseViewState {
		STATE_PROGRESS(1),
		STATE_ERROE(2),
		STATE_EMPTY(3),
		STATE_SUCESS(4);
		private int value;
		BaseViewState(int value){
			this.value = value;
		}
		public int getValue(){
			return value;
		}
	}

	public abstract View obtionView();
	

	public abstract Object obtionData() ;
	
}
